package com.gec.it.web;


import com.gec.it.entity.Depts;
import com.gec.it.service.DeptsService;
import com.gec.it.util.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 欧阳
 * @since 2022-01-04
 */
@Controller
@RequestMapping("/depts")
public class DeptsController {

    @Autowired
    private DeptsService service;


    @GetMapping("/dept_add")
    public String add(){
        return "dept_add";
    }

    @PostMapping("/dept_add")
    public String add(Depts dept){
        service.save(dept);
        return "redirect:dept_list";
    }

    @GetMapping("/dept_list")
    public String deptList(Model m){
        final List<Depts> depts = service.list();
        m.addAttribute("list",depts);
        return "dept_list";
    }

    @GetMapping("/dept_edit")
    public String edit(int id,Model m){
        final Depts dept = service.getById(id);
        m.addAttribute("d",dept);
        return "dept_edit";
    }

    @PostMapping("/dept_edit")
    public String edit(int id,Depts d){
        System.out.println(d);
        d.setId(id);
        service.updateById(d);
        return "redirect:dept_list";
    }

    @GetMapping("/dept_del")
    public String del(int id){
        service.removeById(id);
        return "redirect:dept_list";
    }
}
